package com.dycx.food.converter;

import cn.hutool.core.bean.BeanUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.food.entity.GoodsDO;
import com.dycx.food.vo.GoodsVO;
import com.dycx.food.dto.GoodsDTO;

/**
 * 商品详细表(Goods)实体转换类
 *
 * @author wuzongbing
 * @since 2025-02-13 03:34:42
 */


@Data
public class GoodsConverter implements Serializable {
    public static GoodsDO dtoToEntity(GoodsDTO dto) {
        if (dto == null) return null;
        GoodsDO entity = BeanUtil.toBean(dto, GoodsDO.class);
        return entity;
    }

    public static GoodsVO entityToVo(GoodsDO entity) {
        if (entity == null) return null;
        GoodsVO vo = BeanUtil.toBean(entity, GoodsVO.class);

        return vo;
    }

    public static List<GoodsVO> entityToVoList(List<GoodsDO> entityList) {
        List<GoodsVO> voList = new ArrayList<>();
        for (GoodsDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<GoodsVO> entityToVoPage(Page<GoodsDO> page) {

        List<GoodsVO> newRecords = new ArrayList<>();

        List<GoodsDO> records = page.getRecords();
        for (GoodsDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<GoodsVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
